package com.dublbo.addressbook.frame.group;

import com.dublbo.addressbook.frame.CPersonMainPanel;
import com.dublbo.jpSwing.JpButton;
import com.dublbo.jpSwing.JpPanel;
import com.dublbo.jpSwing.layout.JpColumnLayout;
import com.dublbo.jpSwing.layout.JpRowLayout;

import javax.swing.*;
import java.awt.*;

public class GroupDeleteDialog extends JDialog {
    private final CPersonMainPanel ui;
    private final JRadioButton selectBtn = new JRadioButton("将该组所有联系人从系统中彻底删除");
    private boolean ok;  // 是否确定删除
    private final JpButton okBtn = new JpButton("确定");
    private final JpButton cancelBtn = new JpButton("取消");


    public GroupDeleteDialog(CPersonMainPanel ui) {
        this.ui = ui;
        this.ok = false;
        this.setModal(true);
        this.setSize(300, 200);

        JpPanel root = new JpPanel();
        this.setContentPane(root);
        root.setLayout(new BorderLayout());
        root.padding(5);
        this.setBackground(new Color(0xFFE4E1));

        // 中间面板
        root.add(initCenter(), BorderLayout.CENTER);
        root.add(initBottom(), BorderLayout.PAGE_END);
    }

    private JComponent initCenter() {
        JpPanel panel = new JpPanel();
        panel.setLayout(new JpColumnLayout(2));
        panel.padding(2);

        JLabel l1 = new JLabel("确定要删除该组吗？");
        l1.setFont(new Font("微软雅黑", Font.BOLD, 16));
        JLabel l2 = new JLabel("提示：该组的联系人不会删除！");
        l2.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        l2.setForeground(Color.gray);
        panel.add(l1);
        panel.add(l2);

        selectBtn.setForeground(Color.red);
        selectBtn.setOpaque(false);
        selectBtn.setBorder(null);
        panel.add(selectBtn);

        return panel;
    }

    private JComponent initBottom() {
        JpPanel panel = new JpPanel();
        panel.setLayout(new JpRowLayout(4));
        panel.padding(2);
        panel.preferredHeight(30);

        panel.add(new JLabel(), "1w");
        panel.add(okBtn);
        panel.add(cancelBtn);

        okBtn.addActionListener((e) -> {
            this.ok = true;
            this.setVisible(false);
        });
        cancelBtn.addActionListener((e) -> this.setVisible(false));

        return panel;
    }

    public int exec() {
        // 相对owner居中显示
        Rectangle rect = ui.getBounds();
        int width = this.getWidth();
        int height = this.getHeight();
        int x = rect.x + (rect.width - width) / 2;
        int y = rect.y + (rect.height - height) / 2;
        System.out.println(x + " " + y + " " + width + " " + height);
        this.setBounds(x, y, width, height);
        // 显示窗口 ( 阻塞 ，直到对话框窗口被关闭)
        this.setVisible(true);

        // 此处阻塞 ... 等待用户点 OK，或关闭窗口才会往下执行 ...
        if (this.ok) {
            if (this.selectBtn.isSelected()) {
                return 2;
            } else {
                return 1;
            }
        } else {
            return 0;
        }
    }

}
